package com.test.one.springboottestpne.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.one.springboottestpne.entity.News;
import com.test.one.springboottestpne.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：vue 后台测试类
 * 作者：qpx
 * 创建时间：2018-10-15
 */
@RestController
public class NewsController {


    @Autowired
    NewsService newsService;

    @RequestMapping("/vue/news")
    public Map<String,Object> getNews(News newsFrom){

            PageHelper.startPage(newsFrom.getPageNum(),newsFrom.getPageSize());
            Map<String,Object> map = new HashMap<String,Object>();
            News news = new News();
            news.setStatus(1);
            List<News> newsList = newsService.selectByCondition(news);
            PageInfo<News> pageInfo = new PageInfo<News>(newsList);
            map.put("result",newsList);
            map.put("total",pageInfo.getTotal());
            return map;
    }

    @RequestMapping("/vue/news/del")
    public Map<String,String> delNews(Integer id){
        Map<String,String> map = new HashMap<>();
        map.put("code","100000");
        map.put("msg","删除成功");
        News news = new News();
        news.setId(id);
        news.setStatus(0);
        try {
            newsService.updateByPrimaryKeySelective(news);

        }catch (Exception e){
            e.printStackTrace();
            map.put("code","100001");
            map.put("msg","删除失败");
        }
        return map;
    }

}
